﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MDIDemo
{
    public partial class frmConnect : DevExpress.XtraEditors.XtraForm
    {
        string sErr = "";
        public frmConnect()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (BUS.CommonControl.TestConnection(txtServer.Text, txtUserID.Text, txtPassword.Text, txtDatabase.Text))
            {
                Properties.Settings.Default.ConnectString = String.Format("{0}={1};{2}={3};{4}={5};{6}={7}", txtServer.Tag, txtServer.Text, txtDatabase.Tag, txtDatabase.Text, txtUserID.Tag, txtUserID.Text, txtPassword.Tag, txtPassword.Text);
                Properties.Settings.Default.Save();
                DialogResult = System.Windows.Forms.DialogResult.OK;
                Close();
            }
            else lbErr.Text = "Connect Fail";           
        }

        private void frmConnect_Load(object sender, EventArgs e)
        {
            string str = Properties.Settings.Default.ConnectString;
            string[] arrAtt = str.Split(';');
            foreach (string att in arrAtt)
            {
                string[] value = att.Split('=');
                if (value.Length == 2)
                {
                    switch (value[0])
                    {
                        case "Data Source":
                            txtServer.Text = value[1];
                            break;
                        case "Initial Catalog":
                            txtDatabase.Text = value[1];
                            break;
                        case "User ID":
                            txtUserID.Text = value[1];
                            break;
                        case "Password":
                            txtPassword.Text = value[1];
                            break;
                        
                    }
                }
            }

        }
    }
}